package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Company;
import com.beans.Position;
import com.beans.UPHistory;
import com.beans.User;
import com.service.UserManager;
import com.vo.Page;

public class PositionController extends UniversalController {
	private UserManager userManager;
	
	public UserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public ModelAndView addViewPosition(HttpServletRequest request,
			HttpServletResponse response) {
		String id=request.getParameter("id");
		Map map=new HashMap();
		if(id!=null&&!"".equals(id)){
			Position p=(Position)getUniversalManager().findByID(Position.class, Integer.parseInt(id));
			map.put("o", p);
		}
		return new ModelAndView("company/addPosition",map);
	}
	public ModelAndView addPosition(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		try {
			Object obj = this.getObjectFromJson(request, response, this.obj);
			Position p=(Position)obj;
			
			Company c=(Company)request.getSession().getAttribute("company");
			p.setCompany(c);
			if(p.getId()==0)
				getUniversalManager().insert(p);
			else
				getUniversalManager().update(p);
			map.put("success", true);
			map.put("o", p);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		return new ModelAndView("company/addPosition", map);
	}
	public ModelAndView queryList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		Position p=(Position)getObjectFromJson(request, response, this.obj);
		Page page = getUserManager().getPositionDao().queryList(this.getPageNo(request), pageSize, p);
		result.put("o", page);
		return new ModelAndView("company/queryPositionlist", result);
	}
	public ModelAndView myList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		Company c=(Company)request.getSession().getAttribute("company");
		Position p=new Position();
		bind(request,p);
		p.setCompany(c);
		Page page = getUserManager().getPositionDao().queryList(this.getPageNo(request), pageSize, p);
		result.put("o", page);
		return new ModelAndView("company/myPositionlist", result);
	}
	public ModelAndView view(HttpServletRequest request,HttpServletResponse response){
		User u=(User)request.getSession().getAttribute("user");
		Map result = new HashMap();
		String id=request.getParameter("id");
		Position p=new Position();
		p.setId(Integer.parseInt(id));
		Page page = getUserManager().getPositionDao().queryList(1,1, p);
		if(page.getDataList().size()==1)
		result.put("o", page.getDataList().get(0));
		if(u!=null){
			UPHistory uph=new UPHistory();
			uph.setPositionid(p.getId());
			uph.setUserid(u.getId());
			getUniversalManager().insert(uph);
		}
		return new ModelAndView("company/viewPosition", result);
	}
	public ModelAndView delPosition(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id=request.getParameter("id");
		Position p=null;
		if(id!=null){
			 p=(Position)getUniversalManager().findByID(Position.class, Integer.parseInt(id));
			 getUniversalManager().delete(p);
		}
		getMessage(response, "删除成功！");
		return null;
	}
}
